package com.ahm.consumer.service.OrderServiceImpl;

import com.ahm.consumer.pojo.Order;
import com.ahm.consumer.pojo.Product;
import com.ahm.consumer.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author anhaimeng
 * @title: OrderServiceImpl
 * @projectName eurekademo002
 * @description: TODO
 * @date 2021/10/199:16
 */
/*功能描述
 * @author ahm
 */
@Service
public class OrderServiceImpl3 implements OrderService {

    @Autowired
    private RestTemplate restTemplate;


    @Override
    public Order selectOrderById(Integer id) {
        return new Order(id, "order-001", "中国", 31994D, selectProductListByLoadBalancedAnnotation());
    }

    private List<Product> selectProductListByLoadBalancedAnnotation(){

        // url 中直接写的就是 服务名 自动映射成 host+端口 加了@LoadBalanced注解
        ResponseEntity<List<Product>> exchange = restTemplate.exchange("http://service-provider/product/list", HttpMethod.GET, null, new ParameterizedTypeReference<List<Product>>() {
        });
        return exchange.getBody();

    }





}
